package Be_greedy_for_snakes;

import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

public class RewardLevel extends MainFramer {
    private Obstacle food;//在奖励关中所有的障碍物变成了食物
    private Word food1;//所有的字母也都变成了食物，再也不怕吃错东西了，轻松得分
    private Snake snake;//奖励关中的蛇，长度继承进入奖励关之前的长度
    private Timer timer;   //新定时器，蛇将在奖励关中度过一段时间
    private JPanel jPanel;

    public RewardLevel() {
        initPanel();//初始化游戏棋盘
        initSnake();//初始化蛇
        initTimer();//初始化定时器
        //initMusic();//初始化音乐
        setKeyboardcontrol();//设置键盘控制
        initFood();//初始化第一种食物
        initFood1();//初始化第二种食物
    }

    private void initFood() {
        food = new Obstacle();
        food.random();
    }

    private void initFood1() {
        food1 = new Word();
        food1.random();
        while (true) {//但还是要防止两种食物相互重叠
            int flag = 0;
            LinkedList<Node> o = food.getObstacle();
            LinkedList<Node> w = food1.getWord();
            for (Node node : w) {
                for (Node node1 : o) {
                    if (node1.getX() == node.getX() && node1.getY() == node.getY()) {
                        flag = 1;
                        food1.random();
                        break;
                    }
                }
                if (flag == 1) {
                    break;
                }
            }
            if (flag == 0) {
                break;
            }
        }
    }
/*
    public void initMusic() {                   //播放背景音乐的方法
        Music music = new Music("src/Be_greedy_for_snakes/manhua.wav");
        music.start();
    }

 */

    private void initSnake() {
        snake = getSnake();
    }


    private void initTimer() {                 //定时器方法
        timer = new Timer();
        //初始化定时任务
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                snake.move();

                Node first = snake.getBody().getFirst();
                //吃第一种食物
                if (food != null) {
                    if (food.getObstacle() != null) {
                        LinkedList<Node> fd = food.getObstacle();
                        for (Node node : fd) {
                            if (first.getX() == node.getX() && first.getY() == node.getY()) {
                                snake.eat(node);
                            }
                        }
                    }
                }
                //吃第二种食物
                if (food1 != null) {
                    if (food1.getWord() != null) {
                        LinkedList<Node> fd = food1.getWord();
                        for (Node node : fd) {
                            if (first.getX() == node.getX() && first.getY() == node.getY()) {
                                snake.eat(node);
                            }
                        }
                    }
                }
                jPanel.repaint();      //重绘棋盘，每移动一个节点，就重绘一次棋盘
            }
        };
        timer.scheduleAtFixedRate(timerTask, 0, 60);
    }

    private void initPanel() {
        jPanel = new JPanel() {                 //jpanel对象
            @Override                          //paint可以绘制棋盘中的内容
            public void paint(Graphics g) {
                //绘制侧边框内容
                Font font = new Font("微软雅黑", Font.BOLD, 20);//设置字母的大小和样式
                g.setFont(font);
                g.setColor(Color.RED);//设置字体的颜色
                g.drawString("欢迎来到奖励关卡", 52 * 20, 1 * 20);
                g.drawString("在这里你将有十秒钟的时间:", 52 * 20, 2 * 20);

                //背景
                Image img1 = this.getToolkit().getImage("src/Be_greedy_for_snakes/background.png");
                g.drawImage(img1, 0, 0, 1020, 700, this);//绘制图片方法

                //绘制蛇的身体
                LinkedList<Node> body = snake.getBody();     //通过getBody方法拿到集合
                for (Node node : body) {                     //对集合遍历
                    if (node == body.getFirst()) {//取出头结点,对蛇头朝向的四个方向分别插入四张不同的图片
                        if (snake.getDirection() == Direction.LEFT) {
                            Image img2 = this.getToolkit().getImage("src/Be_greedy_for_snakes/headleft.jpg");
                            g.drawImage(img2, node.getX() * 20, node.getY() * 20, 20, 20, this);
                        } else if (snake.getDirection() == Direction.RIGHT) {
                            Image img2 = this.getToolkit().getImage("src/Be_greedy_for_snakes/headright.jpg");
                            g.drawImage(img2, node.getX() * 20, node.getY() * 20, 20, 20, this);
                        } else if (snake.getDirection() == Direction.UP) {
                            Image img2 = this.getToolkit().getImage("src/Be_greedy_for_snakes/headup.jpg");
                            g.drawImage(img2, node.getX() * 20, node.getY() * 20, 20, 20, this);
                        } else if (snake.getDirection() == Direction.DOWN) {
                            Image img2 = this.getToolkit().getImage("src/Be_greedy_for_snakes/headdown.jpg");
                            g.drawImage(img2, node.getX() * 20, node.getY() * 20, 20, 20, this);
                        }
                    } else {
                        Image img3 = this.getToolkit().getImage("src/Be_greedy_for_snakes/body.jpg");
                        g.drawImage(img3, node.getX() * 20, node.getY() * 20, 20, 20, this);//绘制图片方法
                    }
                }

                //绘制第一种食物
                LinkedList<Node> fd = food.getObstacle();
                for (Node node : fd) {
                    Image img2 = this.getToolkit().getImage("src/Be_greedy_for_snakes/food.png");
                    g.drawImage(img2, node.getX() * 20, node.getY() * 20, 20, 20, this);
                }

                //绘制第二种食物
                LinkedList<Node> fd1 = food1.getWord();
                for (Node node : fd1) {
                    Image img2 = this.getToolkit().getImage("src/Be_greedy_for_snakes/food.png");
                    g.drawImage(img2, node.getX() * 20, node.getY() * 20, 20, 20, this);
                }
            }
        };
        add(jPanel);//将绘制的内容添加到窗口中
    }
}